package com.Servlet;

import com.Bean.User;
import com.Util.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet(urlPatterns = {"/signup"})
public class signup extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {


        req.setCharacterEncoding("utf-8");
        System.out.println(req.getParameter("name"));
        User user = new User(req.getParameter("username"),
                req.getParameter("passwd"), req.getParameter("familyname"),
                req.getParameter("name"), req.getParameter("email"));
        user.setStaff("client");
        UserDao.updateUser(user);

        Map<String, String> message = new LinkedHashMap<String, String>();
        message.put("用户名", user.getUsername());
        message.put("姓", user.getFamilyname());
        message.put("名", user.getName());
        message.put("邮箱", user.getEmail());
        HttpSession session = req.getSession(true);
        if (req.getParameter("net_auto_login") != null)
            session.setMaxInactiveInterval(7 * 24 * 60 * 60);
        session.setAttribute("Login", message);
        session.setAttribute("username", user.getUsername());

            System.out.println("about to dispatch");
            res.sendRedirect("./serve/myvps.jsp");


    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
